﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using Caliburn.Core.InversionOfControl;
using Caliburn.PresentationFramework.RoutedMessaging;
using Caliburn.PresentationFramework.Screens;
using Caliburn.ShellFramework.Results;
using Caliburn.ShellFramework.Questions;
using MTGenome.MTG;

namespace MTGenome.ViewModels
{
    [Singleton(typeof(DeckBuilderViewModel)), PartCreationPolicy(CreationPolicy.Shared)]
    public sealed class DeckBuilderViewModel : ScreenBase
    {
        private readonly string _path,
            _pathDefault;
        private readonly FilterViewModel _filter = null;
        private readonly SearchViewModel _search = null;
        private readonly CardDetailViewModel _cardDetail = null;
        private readonly DeckViewModel _deck = null;
        private readonly CardCollectionViewModel _cardCollection = null;
        private readonly CardImageViewModel _cardImage = null;
        private readonly DeckStatsViewModel _deckStats = null;
        private AvalonDock.DockingManager _manager = null;

        public DeckBuilderViewModel(
            IGathererServer gatherer,
            MainWindowViewModel mainWindow, 
            FilterViewModel filter, 
            SearchViewModel search, 
            CardDetailViewModel cardDetail,
            DeckViewModel deck,
            CardCollectionViewModel cardCollection,
            DeckStatsViewModel deckStats,
            CardImageViewModel cardImage)
            : base(mainWindow)
        {
            _filter = filter;
            _search = search;
            _cardDetail = cardDetail;
            _deck = deck;
            _cardCollection = cardCollection;
            _cardImage = cardImage;
            _deckStats = deckStats;
            DisplayName = "Deck Builder";

            _path = gatherer.DataDirectory + "\\DeckBuilder.layout";
            _pathDefault = _path + ".default";

            MainWindow.Deactivated += (sender, e) =>
                {
                    if (_manager != null && _manager.IsLoaded)
                    {
                        _manager.SaveLayout(_path);
                    }
                };
        }

        public FilterViewModel Filter
        {
            get { return _filter; }
        }

        public SearchViewModel Search
        {
            get { return _search; }
        }

        public CardDetailViewModel CardDetail
        {
            get { return _cardDetail; }
        }

        public CardCollectionViewModel CardCollection
        {
            get { return _cardCollection; }
        }

        public CardImageViewModel CardImage
        {
            get { return _cardImage; }
        }

        public DeckStatsViewModel DeckStats
        {
            get { return _deckStats; }
        }

        public DeckViewModel Deck
        {
            get { return _deck; }
        }

        public void Loaded(AvalonDock.DockingManager manager)
        {
            _manager = manager;
            LoadLayout(_path, manager);
        }

        public IEnumerable<IResult> RestoreDefaults()
        {
            if (_manager == null)
            {
                throw new InvalidOperationException("Could not restore defaults!");
            }

            var mbResult = Show.MessageBox("Are you sure you want to restore the default layout?", "Restore?", a =>
                {
                    if (a == Answer.Yes)
                    {
                        LoadLayout(_pathDefault, _manager);
                    }
                }, Answer.Yes, Answer.No);
            yield return mbResult;
        }

        private void LoadLayout(string path, AvalonDock.DockingManager manager)
        {
            try
            {
                if (System.IO.File.Exists(path))
                {
                    manager.RestoreLayout(path);
                }
                else if (path == _path)
                {
                    //Save our default layout, so we can restore
                    manager.SaveLayout(_pathDefault);
                }
            }
            catch
            { }
        }
    }
}
